/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: WaitForPlayerScreen.java 639 2010-07-30 05:53:30Z Harry $
 *   $LastChangedDate: 2010-07-30 05:53:30 +0000 (Fri, 30 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.screen;

import battlefield.resource.Graphics;
import battlefield.resource.Music;
import battlefield.entity.Player;
import battlefield.ui.component.NormalWhiteButton;
import battlefield.ui.component.VsHardButton;
import battlefield.ui.component.VsNormalButton;
import battlefield.util.ExceptionUtils;
import battlefield.util.ComponentDecorator;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.Timer;

/**
 *
 * @author Yong Peng
 */
public class WaitForPlayerScreen extends Screen implements ActionListener {

    JLabel waitForPlayerLab;
    JLabel firstPlayerLab;
    JLabel secondPlayerLab;
    JTextField firstPlayerText;
    JTextField secondPlayerText;
    JButton startGameBut;
    VsHardButton vsHardButton;
    VsNormalButton vsNormalButton;
    ButtonGroup comButtonGroup;
    private int difficultyLevel;
    private boolean vsComputer;
    private Timer t;

    public WaitForPlayerScreen() {
        initComponents();
    }

    private void initComponents() {
        comButtonGroup = new ButtonGroup();
        waitForPlayerLab = new JLabel(new ImageIcon(Graphics.SCREEN_TITLE_WAIT_FOR_PLAYERS.getImage()));
        vsHardButton = new VsHardButton();
        vsHardButton.addActionListener(this);
        comButtonGroup.add(vsHardButton);
        vsNormalButton = new VsNormalButton();
        vsNormalButton.addActionListener(this);
        comButtonGroup.add(vsNormalButton);
        firstPlayerLab = ComponentDecorator.decorateLabel(firstPlayerLab, "You");
        secondPlayerLab = ComponentDecorator.decorateLabel(secondPlayerLab, "Your Opponent");
        firstPlayerText = ComponentDecorator.decorateTextfield(firstPlayerText, 15, getPlayer().getName());
        firstPlayerText.setEnabled(false);
        secondPlayerText = ComponentDecorator.decorateTextfield(firstPlayerText, 15, "");
        secondPlayerText.setEnabled(false);
        startGameBut = new NormalWhiteButton("Start Game", this);
        startGameBut.setPreferredSize(new Dimension(115, 35));
        startGameBut.setVisible(false);
        vsComputer = false;
        this.setBackgroundImage(Graphics.BACKGROUND_MISC.getImage());
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(
                layout.createSequentialGroup().addGap(50, 50, 50).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(waitForPlayerLab).addGroup(layout.createSequentialGroup().addComponent(firstPlayerLab).addComponent(firstPlayerText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addComponent(secondPlayerLab, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(secondPlayerText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(startGameBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(50, 50, 50).addGroup(layout.createSequentialGroup().addGap(50, 50, 50).addComponent(vsNormalButton).addComponent(vsHardButton))));
        layout.linkSize(firstPlayerLab, secondPlayerLab);
        layout.setVerticalGroup(
                layout.createSequentialGroup().addGap(50, 50, 50).addComponent(waitForPlayerLab).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(firstPlayerLab).addComponent(firstPlayerText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(secondPlayerLab, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(secondPlayerText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(startGameBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(50, 50, 50).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(50, 50, 50).addComponent(vsNormalButton).addComponent(vsHardButton)));
        t = new Timer(1000, this);
        t.start();
    }

    /* Begin Region: State Pattern */
    @Override
    public Screen startTactics() {
        return new TacticalPhaseScreen();
    }
    /* End Region: State Pattern */

    private void addComputerPlayer(int difficultyLevel) {

        try {
            getLogic().computerJoinGame(getPlayer().getId(), getGame().getId(), difficultyLevel);
        } catch (RemoteException ex) {
            Logger.getLogger(WaitForPlayerScreen.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(t)) {
            try {
                if (getLogic().isOpponentJoin(getPlayer().getId())) {
                    t.stop();
                    Player player = getOpponent(true);
                    secondPlayerText.setText(player.getName());
                    if (getLogic().isHost(getPlayer().getId())) {
                        getLogic().setGameSettings(getGame().getId(), getSession().getGoldReloadDuration(), getSession().getGoldReloadAmount(), getSession().getStartGold());
                    }
                    startGameBut.setVisible(true);
                    vsNormalButton.setVisible(false);
                    vsHardButton.setVisible(false);
                    vsComputer = false;
                }
            } catch (RemoteException ex) {
                JOptionPane.showMessageDialog(this, ExceptionUtils.getMessage(ex));
            }
        } else if (e.getSource().equals(startGameBut)) {
            if (vsComputer) addComputerPlayer(difficultyLevel);
            try {
                if (getLogic().isHost(getPlayer().getId())) {
                    getLogic().setGameSettings(getGame().getId(), getSession().getGoldReloadDuration(), getSession().getGoldReloadAmount(), getSession().getStartGold());
                }
            } catch (RemoteException ex) {
                Logger.getLogger(WaitForPlayerScreen.class.getName()).log(Level.SEVERE, null, ex);
            }
            ImageIcon newBackImage;
            Image newImage;
            newBackImage = new ImageIcon(Graphics.SCREEN_LOADING.getImage());
            newImage = newBackImage.getImage();
            this.getGraphics().drawImage(newImage, 0, 0, this);
            //Music.LOADING.start();
            Music.GAME_MENU.stop();
            Music.LOADING.start();

            getGUI().startTactics();
        } else if (e.getSource().equals(vsNormalButton)) {
            if (secondPlayerText.getText().equalsIgnoreCase("Com_Normal")) {
                vsComputer = false;
                secondPlayerText.setText("");
                startGameBut.setVisible(false);
            } else {
                vsComputer = true;
                difficultyLevel = 0;
                secondPlayerText.setText("Com_Normal");
                startGameBut.setVisible(true);
            }
        } else if (e.getSource().equals(vsHardButton)) {
            if (secondPlayerText.getText().equalsIgnoreCase("Com_Hard")) {
                vsComputer = false;
                secondPlayerText.setText("");
                startGameBut.setVisible(false);
            } else {

                vsComputer = true;
                difficultyLevel = 1;
                secondPlayerText.setText("Com_Hard");
                startGameBut.setVisible(true);
            }
        }
    }

    @Override
    protected void finalize() throws Throwable {
        if (t != null && t.isRunning()) {
            t.stop();
        }
        t = null;
        super.finalize();
    }
}
